from datetime import datetime

from flask import Flask, render_template, Response, make_response, jsonify, url_for, session, redirect, request
from flask_sqlalchemy import SQLAlchemy
from DB import db
from apps.controller.LoginController import login_blue
from apps.controller.UserController import user_bp
# from apps.pojo.User import User
from apps.service.LoginService import LoginService

app = Flask(__name__)
#这个相当于路由register_blueprint
app.register_blueprint(user_bp)
app.register_blueprint(login_blue)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:shiyunlai123@localhost:3306/flask'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # 禁用信号，优化性能
db.init_app(app)
app.secret_key = 'your_secret_key'
num=100

# db={
#     'user':'root',
#     'password':'shiyunlai123',
#     'host':'localhost',
#     'port':'3306',
#     'database':'flask'
# }

# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:shiyunlai123@localhost:3306/flask'
#





# @app.route('/database')
# def database():
#     users = User.query.all()  # 获取所有用户
#     print(users)
#     # return str(users)
#     return f'你好：{users}'
# @app.route('/')  # 根路径
# def hello_world():
#     resp=Response(response='Python&Flask',status=201,
#                                  content_type='text/html;charset=utf-8')
#
#     user1={'name':'John','age':'1234'}
#     user2={'name':'sdds','age':'12'}
#     list=[user1,user2]
#     res=make_response(jsonify(list), status=202)
#
#     return res

@app.route('/')
def login():
    return render_template('login.html')
@app.route('/hello/flask')
def greet():
    return f"{url_for('greet')}"   # 反向解析视图函数greet()对应的URL


# @app.route('/main/<num>')  # 根路径
# def main(num):
#     return render_template('index.html',num=num)
#     # return num+' '


# @app.route('/index')
# def index():
#     if 'username' in session:
#         return f'你好：{session.get("username")}'  #返回欢迎信息
#     return redirect(url_for("main",num=200))

# @app.route('/login')  # 根路径
# def login():
#     return render_template('login.html')

# @app.route('/dologin',methods=['POST'])  # 根路径
# def dologin():
#     username = request.form.get('username')
#     password = request.form.get('password')
#     boolean=LoginService.login(username, password)
#     if boolean:
#         return render_template('index.html')
#     else:
#         return '用户名或密码错误'

    # result = request.form
    # print(result)
    # return result;


@app.route('/login', methods=['POST'])
def dologin():
    param = request.form
    username = param.get('username')
    password = param.get('password')
    # 调用登录方法
    user = LoginService.login(username, password)
    if user:
        return render_template('index.html', user=user)
    else:
        return render_template('login.html', msg='用户名或密码错误')

@app.route('/index_v1')
def index_v1():
    return render_template('index_v1.html')  # 渲染模板文件


@app.route('/index_v3')
def index_v3():
    return render_template('index_v3.html')



if __name__ == '__main__':
    # 运行app，默认运行在5000
    # 默认是host='127.0.0.1', port=5000端口
    # app.run()
    app.run(debug=True, port=5000, host='0.0.0.0')

'运行后http://127.0.0.1:5000访问这个地址，即可看到hello，world了'

